package com.leyou.item.service;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.leyou.common.enums.ExceptionEnum;
import com.leyou.common.exception.LyException;
import com.leyou.common.utils.BeanHelper;
import com.leyou.common.vo.PageResult;
import com.leyou.item.entity.Brand;
import com.leyou.item.mapper.BrandMapper;
import com.leyou.item.pojo.BrandDTO;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;
import tk.mybatis.mapper.entity.Example;

import java.util.ArrayList;
import java.util.List;


@Service
public class BrandService {
    @Autowired
    BrandMapper brandMapper;

    public Object queryBrandByPage(Integer page, Integer rows, String key, String sortBy, Boolean desc) {
        //1 分页
        PageHelper.startPage(page, rows);
        //2 过滤条件
        Example example = new Example(Brand.class);
        if (StringUtils.isNotBlank(key)) {
            example.createCriteria().orLike("name", "%" + key + "%")
                    .orEqualTo("letter", key.toUpperCase());
        }
        //3 排序
        if (StringUtils.isNotBlank(sortBy)) {
            String orderByClause = sortBy + (desc ? " DESC" : " ASC");
            example.setOrderByClause(orderByClause);// id desc
        }
        //4 查询
        List<Brand> brands = brandMapper.selectByExample(example);
        //5  判断是否为空
        if (CollectionUtils.isEmpty(brands)) {
            throw new LyException(ExceptionEnum.BRAND_NOT_FOUND);
        }
        //6 解析分页结果
        PageInfo<Brand> pageInfo = new PageInfo<>(brands);
        // 转为BrandDTO
        List<BrandDTO> list = BeanHelper.copyWithCollection(brands, BrandDTO.class);

        // 返回                这里是需要让pageinfo帮我们计算总条数,所以从pageinfo中去取出total
        //                     pageinfo的总条数不是单单靠我们创建pageinfo时传入的list来计算的,是PageHelper协同计算的
        return new PageResult<>(pageInfo.getTotal(), list);
    }

    public void saveBrand(BrandDTO brandDTO, List<Long> ids) {
        Brand brand = BeanHelper.copyProperties(brandDTO, Brand.class);
        brand.setId(null);
        int count = brandMapper.insertSelective(brand);
        if (count != 1) {
            // 新增失败，抛出异常
            throw new LyException(ExceptionEnum.INSERT_OPERATION_FAIL);
        }
        //
        count = brandMapper.insertCategoryBrand(brand.getId(), ids);
        if (count != ids.size()) {
            // 新增失败，抛出异常
            throw new LyException(ExceptionEnum.INSERT_OPERATION_FAIL);
        }
    }

    public BrandDTO queryById(Long id) {
        Brand brand = brandMapper.selectByPrimaryKey(id);
        if (brand == null) {
            throw new LyException(ExceptionEnum.BRAND_NOT_FOUND);
        }
        return BeanHelper.copyProperties(brand, BrandDTO.class);
    }

    public List<BrandDTO> queryByCategoryId(Long cid) {
        //根据商品分类id获取旗下品牌
        List<Brand> brands = brandMapper.selectBrandByCategoryId(cid);
        List<BrandDTO> brandDTOS = BeanHelper.copyWithCollection(brands, BrandDTO.class);

        if (CollectionUtils.isEmpty(brandDTOS)) {
            throw new LyException(ExceptionEnum.BRAND_NOT_FOUND);
        }
        return brandDTOS;
    }

    public List<BrandDTO> queryBrandByIds(List<Long> idList) {
        List<Brand> brands = brandMapper.selectByIdList(idList);
        if(CollectionUtils.isEmpty(brands)){
            throw new LyException(ExceptionEnum.BRAND_NOT_FOUND);
        }
        return BeanHelper.copyWithCollection(brands,BrandDTO.class);
    }


}
